package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public PageVO<ProductCategoryTreeNodeVO> getCategoryList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //查询出所有1级分类
        ProductCategory category1 = new ProductCategory();
        category1.setPid(Long.parseLong("0"));
        List<ProductCategory> list1 = productCategoryMapper.select(category1);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = list1.stream().map(productCategory1 -> {
            //查询出所有1级分类的2级分类
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
            ProductCategory category2 = new ProductCategory();
            category2.setPid(productCategory1.getId());
            List<ProductCategory> list2 = productCategoryMapper.select(category2);
            BeanUtils.copyProperties(productCategory1, productCategoryTreeNodeVO1);
            productCategoryTreeNodeVO1.setLev(1);
            //1级分类下set进2级分类
            productCategoryTreeNodeVO1.setChildren(list2.stream().map(productCategory2 -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO2 = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory2, productCategoryTreeNodeVO2);
                productCategoryTreeNodeVO2.setLev(2);
                //查询出所有的2级分类下的3级分类
                ProductCategory category3 = new ProductCategory();
                category3.setPid(productCategory2.getId());
                List<ProductCategory> list3 = productCategoryMapper.select(category3);
                //2级分类下set进3级分类
                productCategoryTreeNodeVO2.setChildren(list3.stream().map(productCategory3 -> {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO3 = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(productCategory3, productCategoryTreeNodeVO3);
                    productCategoryTreeNodeVO3.setLev(3);
                    return productCategoryTreeNodeVO3;
                }).sorted(ProductCategoryTreeNodeVO.order()).collect(Collectors.toList()));
                return productCategoryTreeNodeVO2;
            }).sorted(ProductCategoryTreeNodeVO.order()).collect(Collectors.toList()));
            return productCategoryTreeNodeVO1;
        }).sorted(ProductCategoryTreeNodeVO.order()).collect(Collectors.toList());
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(list1);
        return new PageVO<>(pageInfo.getTotal(),productCategoryTreeNodeVOS);
    }

    //获取父类分级属性
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryList() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryList.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
            return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOList);
    }

    //添加分类
    @Override
    public void addProductCategory(ProductCategory productCategory) {
        Calendar calendar = Calendar.getInstance();
        productCategory.setCreateTime(calendar.getTime());
        productCategory.setModifiedTime(calendar.getTime());
        productCategoryMapper.insertSelective(productCategory);
    }

    //编辑分类 数据回显
    @Override
    public ProductCategory editProductCategory(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    //修改分类
    @Override
    public void updateProductCategory(Long id, ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    //删除分类
    @Override
    public Integer deleteProductCategory(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setPid(id);
        List<ProductCategory> productCategoryList = productCategoryMapper.select(productCategory);
        if (CollectionUtils.isEmpty(productCategoryList)){
            return productCategoryMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }


}
